package com.mj.action.system;


import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.mj.base.BaseController;
import com.mj.model.system.Menu;
import com.mj.service.system.MenuService;
import com.mj.service.system.RoleService;
import com.mj.util.CommonUtil;

/**
 * 菜单管理
 *@ClassName:MenuAction.java
 *@author dsc
 *@date 2017年7月21日
 *@Description TODO
 *
 */
@Controller
@RequestMapping("menu")
public class MenuAction extends BaseController{
	@Resource
	private MenuService menuService;
	
    @Resource
    RoleService roleService;
    @Resource
    CommonUtil commonUtil;
	
    
	
	/**
	 * 菜单列表，grid形式
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="menuGrid")
	public String menuGrid(){
		//菜单名称
		String menuName = request.getParameter("menuName");
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("menuName", menuName);
    	//菜单列表信息
    	List<Menu> menulist = menuService.findAll(map);
    	//是否根据menuname返回list，处理
    	List<Map<String,Object>> mlist = menuService.loadByMenuName(menulist, menuName);
		JSONArray objarr = JSONArray.fromObject(mlist);
        return renderJsonGrid(objarr, mlist.size());
	}
	
	//菜单列表页面
	@RequestMapping(value = "")
	public String toMenuList(Model model){
		return "system/menu/menu_list"; 
	}
	
	/**
	 * 树菜单
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getMenuComTree")
	public String getMenuComTree(Model model){
		List<Menu> menulist = menuService.findAll(null);
		List<Map<String,Object>> ml = commonUtil.treeList(menulist, 0);
		return renderJsonArray(ml); 
	}
	
	
	/**
	 * 添加菜单
	 * @param menu
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="addMenu")
	public Boolean addMenu(Menu menu){
		int flag = menuService.insertMenu(menu);
		if(flag > 0){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 软删除菜单
	 *  
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="delMenu")
	public Boolean delMenu(){
		int menuId =Integer.parseInt(request.getParameter("menu_id"));
		int available =Integer.parseInt(request.getParameter("available"));
		int flag = menuService.updateByMenuId(menuId,available);
		if(flag > 0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 编辑菜单信息
	 * @param menu
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="editMenu")
	public Boolean editMenu(Menu menu){
		int flag = menuService.updateMenu(menu);
		if(flag > 0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 查询菜单
	 */
	@ResponseBody
	@RequestMapping(value="getMenuById")
	public String getMenuById(){
		Menu u = menuService.selectById(Integer.valueOf(request.getParameter("uid")));
		return renderJson(u);
	}
	
	/**
	 * 拖拽菜单
	 */
	@ResponseBody
	@RequestMapping(value="dragMenu")
	public void dragMenu(Menu menu){
	}
	
	
	@RequestMapping(value = "roleMenuBind", method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String menuBind() {
		gson = new Gson();
		List<Menu> menus = new ArrayList<>();
		try {
			menus = menuService.findAllMenuButtons();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("数据查询失败", e);
		}
		Menu menu = new Menu();
		menu.setMenuId(0);
		menu.setMenuName("系统菜单");
		menus.add(menu);
		
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("total", menus.size());
		jsonMap.put("rows", menus);
		gson = new GsonBuilder().registerTypeAdapter(Menu.class,
				new JsonSerializer<Menu>() {
					@Override
					public JsonElement serialize(Menu arg0, Type arg1,
							JsonSerializationContext arg2) {
						JsonObject json = new JsonObject();
						json.addProperty("id", arg0.getMenuId());
						json.addProperty("name",arg0.getMenuName());
						json.addProperty("_parentId", arg0.getParentMenuId());
						if (arg0.getButtonsIds() != null && !arg0.getButtonsIds().equals("")) {
							String btnames[] = arg0.getButtonsIds().split(",");
							for (int i = 0; i < btnames.length; i++) {
								json.addProperty("A" + (i + 1), btnames[i]);
							}
						}
						return json;
					}
				}).create();
		return gson.toJson(jsonMap);
	}

	
	
}
